package com.future.mayi.view;

import android.content.Context;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.DateUtils;
import com.future.mayi.R;
import com.future.mayi.model.IndexTimeModel;
import com.future.mayi.model.interfaces.IndexTimeListener;

import butterknife.BindView;
import butterknife.ButterKnife;

public class IndexTimeView extends LinearLayout {

    @BindView(R.id.ll_time_main)
    View llTimeMain;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_sub_title)
    TextView tvSubTitle;
    @BindView(R.id.tv_h)
    TextView tvH;
    @BindView(R.id.tv_m)
    TextView tvM;
    @BindView(R.id.tv_s)
    TextView tvS;

    private Context mContext;


    private IndexTimeModel model;
    private IndexTimeListener listener;

    private CountDownTimer timer;

    public IndexTimeView(Context context) {
        this(context, null);
    }

    public IndexTimeView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndexTimeView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        inflate(context, R.layout.layout_index_time, this);
        ButterKnife.bind(this, this);
        setLayoutParams();
        setModel(model, listener);
    }

    public void setModel(IndexTimeModel model, IndexTimeListener listener) {
        if (model == null || listener == null) return;
        this.model = model;
        this.listener = listener;
        if (tvTitle == null) return;
        tvTitle.setText(model.getTitle() == null ? "" : model.getTitle());
        tvSubTitle.setText(model.getSubTitle() == null ? "" : model.getSubTitle());
        if (model.getRemainSecond() > 0) {
            registerTimer();
        } else {
            unregisterTimer();
            tvH.setText("00");
            tvM.setText("00");
            tvS.setText("00");
        }
    }


    private void registerTimer() {
        unregisterTimer();
        long subTime = model.getRemainSecond() - ((System.currentTimeMillis() - model.getThisTime())) / 1000;
        if (subTime <= 0) return;
        timer = new CountDownTimer(subTime * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                refreshTime();
            }

            @Override
            public void onFinish() {
                refreshTime();
            }
        }.start();
    }


    private void unregisterTimer() {
        if (timer != null)
            timer.cancel();
        timer = null;
    }

    boolean inRefreshTime = false;

    public void refreshTime() {
        if (inRefreshTime) return;
        inRefreshTime = true;
        long subTime = model.getRemainSecond() - ((System.currentTimeMillis() - model.getThisTime()) / 1000);
        if (subTime > 0) {
            String[] time = DateUtils.secToTime(subTime);
            tvH.setText(time[0]);
            tvM.setText(time[1]);
            tvS.setText(time[2]);
        } else {
            listener.timeEnd(this, model);
            unregisterTimer();
            tvH.setText("00");
            tvM.setText("00");
            tvS.setText("00");
        }
        inRefreshTime = false;
    }


    private void setLayoutParams() {
        if (llTimeMain == null || llTimeMain.getLayoutParams() == null) return;
        ViewGroup.LayoutParams layoutParams = llTimeMain.getLayoutParams();
        layoutParams.width = CommonUtils.getScreenSize(mContext)[0];
        llTimeMain.setLayoutParams(layoutParams);
    }

}
